/*INIT*/
$(document).ready(function() {
	historyOrders.init();
});

var rateStar = {
	text: {
		0: '&nbsp;',
		1: '很差',
		2: '差',
		3: '还行',
		4: '好',
		5: '很好'
	},
	init: function() {
		$('[name=r_star] .star div').mouseenter(rateStar.enter).mouseleave(rateStar.leave).click(rateStar.set);
	},
	enter: function() {
		var i = $(this).attr('val');
		$(this).parent().removeClass().addClass('star star_'+i);
		$(this).parents('.rateBlock').find('.dytext').html(rateStar.text[i]);
		$(this).parents('.rateBlock').find('.tips').hide();
	},
	leave: function() {
		var i = $(this).parents('.rateBlock').attr('val');
		$(this).parents('.rateBlock').find('.star').removeClass().addClass('star star_'+i);
		$(this).parents('.rateBlock').find('.dytext').html(rateStar.text[i]);
		if(i == 0) $(this).parents('.rateBlock').find('.tips').show();
	},
	set: function() {
		var i = $(this).attr('val');
		$(this).parents('.rateBlock').attr('val', i);
	}
};

var rateQuality = {
	settings: {
		min: 0,
		length: 4,
		step: 1,
		text: [
			'无',
			'一般',
			'还行',
			'好',
			'很好'
		]
	},
	now: null,
	barX: null,
	startX: null,
	init: function() {
		$('[name=r_quality] .traveler').mousedown(function(e){
			$(this).parents('.rateBlock').find('.tips').hide();
			rateQuality.now = $(this);
			rateQuality.barX = parseInt(rateQuality.now.css('left'));
			rateQuality.startX = e.clientX;
			$(document).mousemove(rateQuality.drag);
			e.preventDefault();
		});
		$(document).mouseup(function(){
			rateQuality.now = null;
			rateQuality.barX = null;
			rateQuality.startX = null;
			$(document).unbind('mousemove', rateQuality.drag);
		});
	},
	drag: function(e) {
		if(rateQuality.now == null) return;
		var l = rateQuality.barX + e.clientX - rateQuality.startX;
		l = l < 0 ? 0 : (l > 95) ? 95 : l;
		rateQuality.now.css('left', l+'px');
		var s = rateQuality.settings;
		var t = s.min + Math.round(l / 95 * s.length / s.step) * s.step;
		rateQuality.now.parents('.rateBlock').attr('val', t).find('.dytext').html(s.text[t]);
	}
};

var rateSpeed = {
	settings: {
		min: 10,
		length: 110,
		step: 5
	},
	now: null,
	barX: null,
	startX: null,
	init: function() {
		$('[name=r_speed] .traveler').mousedown(function(e){
			$(this).parents('.rateBlock').find('.tips').hide();
			rateSpeed.now = $(this);
			rateSpeed.barX = parseInt(rateSpeed.now.css('left'));
			rateSpeed.startX = e.clientX;
			$(document).mousemove(rateSpeed.drag);
			e.preventDefault();
		});
		$(document).mouseup(function(){
			rateSpeed.now = null;
			rateSpeed.barX = null;
			rateSpeed.startX = null;
			$(document).unbind('mousemove', rateSpeed.drag);
		});
	},
	drag: function(e) {
		if(rateSpeed.now == null) return;
		var l = rateSpeed.barX + e.clientX - rateSpeed.startX;
		l = l < 0 ? 0 : (l > 95) ? 95 : l;
		rateSpeed.now.css('left', l+'px');
		l = l*l/95;	//调整速度增加前慢后快
		var s = rateSpeed.settings;
		var t = s.min + Math.round(l / 95 * s.length / s.step) * s.step;
		rateSpeed.now.parents('.rateBlock').attr('val', t).find('.dytext').html(t+'分钟');
	}
};

var rateSubmit = {
	init: function() {
		$('.rateArea [name=submit]').click(rateSubmit.submit);
	},
	submit: function() {
		var order = $(this).parents('.rateArea');
		var star = order.find('[name=r_star]').attr('val');
		var quality = order.find('[name=r_quality]').attr('val');
		var speed = order.find('[name=r_speed]').attr('val');
		if(star == 0 || typeof quality == 'undefined' || typeof speed == 'undefined') {
			alert('请填写完整评价信息');
			return;
		}
		var data = {
			ordersId: $(this).attr('ordersId'),
			totalComment: star,
			productComment: quality,
			deliverComment: speed,
			r_comment: order.find('[name=r_comment]').val()
		};
		//TODO - The Submition Process
		auiAjax.send('filter', {
	        url : getRootPath() + '/customerajax/hotComments!addComment.action',
	        data : data,
	        cache : false,
	        type : 'POST',
	        dataType : 'json',
	        beforeSend	: function(xmlHttpRequest){
	        	order.find('[name=submit]').attr('class', 'btn_91_28_g submit submit_l');
	        	order.find('[name=submit]').html('正在提交');
	        	order.find('[name=submit]').unbind('click');
			},
	        success : function (d){	        	
	        	var commentContent = order.find('[name=r_comment]').val();
	        	commentContent = '<div class="commented"><b>评论内容：</b><em>'+commentContent+'</em></div>';
	        	order.find('[name=r_comment]').after(commentContent);
	        	order.find('[name=r_comment]').remove();
	        	order.find('[name=submit]').attr('class', 'btn_91_28_g submit submit_l');
	        	order.find('[name=submit]').html('已评价');	 
	        	pointsPop.gen(20, order.find('[name=submit]'));
	        	order.find('[name=submit]').removeAttr('name');
	        	order.find('[name=r_star] .star div').unbind('mouseenter');
	        	order.find('[name=r_star] .star div').unbind('mouseleave');
	        	order.find('[name=r_star] .star div').unbind('click');
	        	order.find('[name=r_quality] .traveler').unbind('mouseup');
	        	order.find('[name=r_quality] .traveler').unbind('mousedown');
	        	order.find('[name=r_speed] .traveler').unbind('mouseup');
	        	order.find('[name=r_speed] .traveler').unbind('mousedown');
	        	order.find('.comment_quick').hide();
	        	order.parents('li').find('.listItem.li_6 a:eq(1)').text(' 已评价');
	        },
	        error :	function(){
	        	//关闭提交动画，响应失败请求
	        	alert("error");
	        }
	    });
	}
};

var orderList = {
	init: function() {
		$('.listItem > a').click(orderList.toggle);
		$('.detailArea .fold').click(orderList.toggle);
	},
	toggle: function() {
		var detailArea = $(this).parentsUntil('ul').find('.detailArea');
		if(detailArea.css('display') == 'none') detailArea.show();
		else detailArea.hide();
		if($.browser.msie && $.browser.version < 8) $('.orderArea li').css('zoom', '0').css('zoom', '1');
	}
};

var quickComment = {
	init: function() {
		$('.comment_quick a').click(quickComment.check);
	},
	check: function() {
		//regs := [赞一个,味道不错,我的最爱,速度挺快,一般]
		var regs = [/\u8d5e\u4e00\u4e2a/, /\u5473\u9053\u4e0d\u9519/, /\u6211\u7684\u6700\u7231/, /\u901f\u5ea6\u633a\u5feb/, /\u4e00\u822c/];
		var i = this.innerHTML,
			t = $(this).attr("val"),
			v = $(this).parent().prev().val(),
			reg = regs[t];
		if(reg.test(v)) {
			return;
		} else {
			if(v !== '') {i = ' '+i;}
			$(this).parent().prev().val(v+i);
		}
	}
};

var historyOrders = {
	pageNum: 0,
    init: function() {
//    	$('.partr > .orderArea').each(function(){historyOrders.bind(this);});
    	$('.body .active').click(historyOrders.filter());
    },
    clear: function() {
    	$('.partr').empty();
    },
    add: function(d) {
    	var res = $(new EJS({url: getRootPath() + '/assets/ejs/myOrdersHistory.ejs'}).render(d));
    	$('.partr').append(res);
    },
    filter: function() {
    	auiAjax.send('filter', {
			url : getRootPath() + '/customerajax/myOrders!queryHistoryOrders.action',
			type : 'POST',
			cache : false,
			dataType : 'json',
			data : {pageNum: historyOrders.pageNum},
	        beforeSend	: function(xmlHttpRequest){
	        	loadingArea.attach(".partr");
			},
			success : function(d) {
				loadingArea.remove(".resultList");
//				if(d.errorCode != "") return;
				historyOrders.clear();

				d.rootPath = getRootPath();			
				historyOrders.add(d);
				
				historyOrders.paginator();
				rateStar.init();
				rateQuality.init();
				rateSpeed.init();
				rateSubmit.init();
				orderList.init();
				quickComment.init();
				resetFooter_2();
			},
			error : function() {
			}
		});
    },
    paginator: function() {
      	auiAjax.send('filter', {
			url : getRootPath() + '/customerajax/myOrders!queryHistoryNum.action',
			type : 'POST',
			cache : false,
			dataType : 'json',
			success : function(d) {
				var count = d.count; //总记录数
				var pageSize = d.pageSize; //每页显示订单数
				var pageNum = historyOrders.pageNum+1; //当前页
				
				var pageCount = Math.ceil(count/pageSize); //总页数
				var breakPage = 3; //连续显示页数
				var position = 1;  //== Math.floor(breakPage/2);
				var primacyPage = 1; //首尾显示页数
				var maxSpace = 1; //点号中断的最少页数
				var prevNum = pageNum-position;
				var nextNum = pageNum+position;
				
				var pageStr = "";
				if (prevNum < 1) {prevNum = 1;}
				if (nextNum > pageCount) {nextNum = pageCount;}
				pageStr += (pageNum == 1) ? '<span class="uppage">上一页</span>' : '<a class="uppage" goPage="' +(pageNum -1)+ '">上一页</a>';
				
				if (prevNum-primacyPage>maxSpace) {
				    for (var i = 1;i <= primacyPage;i += 1) {
				    	pageStr += '<a goPage="'+i+'"> '+i+' </a>';
				    }
				    pageStr += '<em class="break">...</em>';
				    for (var i = pageCount-breakPage+1;i < prevNum;i += 1) { 
				        pageStr += '<a goPage="'+i+'"> '+i+' </a>'; 
				    }
				} else {
				    for (var i = 1;i < prevNum;i += 1) {pageStr += '<a goPage="'+i+'"> '+i+' </a>';}
				}

				for (var i = prevNum;i <= nextNum;i += 1) {
				    pageStr += (pageNum==i)?'<b class="thispage"> '+i+' </b>':'<a goPage="'+i+'"> '+i+' </a>';
				}

				if (pageCount-primacyPage-nextNum+1>maxSpace) {
				    for (var i = nextNum+1;i <= breakPage;i += 1) { 
				        pageStr += '<a goPage="'+i+'"> '+i+' </a>'; 
				    }
				    pageStr += '<em class="break">...</em>';
				    for (var i = pageCount-primacyPage+1;i <= pageCount;i += 1) {
				        pageStr += '<a goPage="'+i+'"> '+i+' </a>'; 
				    }
				} else {
				    for (var i = nextNum+1;i <= pageCount;i += 1) { 
				        pageStr += '<a goPage="'+i+'"> '+i+' </a>'; 
				    }
				}
				pageStr += (pageNum==pageCount) ? '<span class="downpage">下一页</span>' : '<a class="downpage" goPage="'+(pageNum+1)+'">下一页</a>';
				$('.pager').append(pageStr);
				$(".pager a").click(function(){historyOrders.gotoPage(this);});
				
			},
			error : function() {
			}
      	});
    },
    gotoPage: function(ele) {
    	var pageNum = $(ele).attr('goPage');
    	historyOrders.pageNum = pageNum-1;
    	historyOrders.filter();
    }
};
